package com.hdu.other.dp.treeDP;

import java.io.*;
import java.util.Arrays;

/**
 * <a href="https://www.luogu.com.cn/problem/P1352">没有上司的舞会</a>
 */
public class luogu {

    private static final int MAX_N = 6001;

    private static final boolean[] ROOT = new boolean[MAX_N];
    private static final int[] HAPPY = new int[MAX_N];

    /**
     * 链式前向星相关
     */
    private static final int[] HEAD = new int[MAX_N];
    private static final int[] TO = new int[MAX_N];
    private static final int[] NEXT = new int[MAX_N];
    private static int CNT = 0;

    private static final int[] YES = new int[MAX_N];
    private static final int[] NO = new int[MAX_N];

    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
        StreamTokenizer st = new StreamTokenizer(reader);

        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) st.nval;
            refresh();
            for (int i = 1; i <= n; i++) {
                st.nextToken();
                HAPPY[i] = (int) st.nval;
            }
            for (int i = 1; i < n; i++) {
                st.nextToken();
                int to = (int) st.nval;
                st.nextToken();
                int from = (int) st.nval;
                addEdge(from, to);
                ROOT[to] = false;
            }
            for (int i = 1; i <= n; i++) {
                if (ROOT[i]) {
                    cal(i);
                    writer.println(
                            Math.max(
                                    YES[i],
                                    NO[i]
                            )
                    );
                    break;
                }
            }
        }

        writer.flush();
        writer.close();
        reader.close();
    }

    static void cal(int x) {

        YES[x] = HAPPY[x];
        NO[x] = 0;

        for (int curE = HEAD[x]; curE != 0; curE = NEXT[curE]) {
            int to = TO[curE];
            cal(to);
            YES[x] += NO[to];
            NO[x] += Math.max(NO[to], YES[to]);
        }
    }

    static void addEdge(int from, int to) {
        NEXT[CNT] = HEAD[from];
        TO[CNT] = to;
        HEAD[from] = CNT++;
    }

    static void refresh() {
        CNT = 1;
        Arrays.fill(HEAD, 0);
        Arrays.fill(ROOT, true);
    }
}
